package u.can.i.up.db.mongo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class MongoStatistic {

	public static void main(String[] args) throws IOException {
		// staticStatistic();
		// virusStatistic2();
		// ruleNumberStatistic();
		// ruleFileStatistic("E:\\distinctRuleNumber.txt");
		// getVirusApk();
		// writeFile(getFileChecksumList1("E:\\实验室\\项目\\数据库\\7月31日取数据\\带毒应用的checksum20000.txt"),
		// "E:\\实验室\\项目\\数据库\\7月31日取数据\\带毒应用的path.txt");
		// File directory = new File("E:\\实验室\\项目\\深圳\\11月\\应用酷");
		// for(File file : directory.listFiles()) {
		// if(file.getName().endsWith(".txt")) {
		// List<String> md5List = getFileChecksumList(file.getAbsolutePath());
		// virusTypeStatistic(file.getName().substring(0,
		// file.getName().lastIndexOf(".")) + ".xls", md5List);
		// }
		// }
		virusStatistic();
	}

	static void staticStatistic() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("android.static.result");
		// BasicDBObject doc=new BasicDBObject();
		BasicDBList checksum = new BasicDBList();
		List<String> fileChecksums = getFileChecksumList("E:\\实验室\\项目\\数据库\\6月17日和游戏统计\\md5.txt");
		System.out.println("md5 共有" + fileChecksums.size());
		for (String fileChecksum : fileChecksums) {
			checksum.add(fileChecksum);
		}
		DBObject doc = new BasicDBObject("$in", checksum);
		BasicDBObject dbObject = new BasicDBObject("MD5_Apk", doc);
		BasicDBList ruleIds = new BasicDBList();
		// ruleIds.add("1-3-1-1");
		// ruleIds.add("1-1-1-3");
		// ruleIds.add("1-2-1-1");
		// ruleIds.add("1-1-1-6");
		// ruleIds.add("1-1-2-1");
		// ruleIds.add("1-1-2-2");
		// ruleIds.add("1-1-2-3");
		// ruleIds.add("1-2-2-1");
		// ruleIds.add("1-2-2-2");
		// ruleIds.add("1-1-1-2");
		// ruleIds.add("1-2-1-2");
		// ruleIds.add("1-2-1-3");
		// ruleIds.add("1-1-3-1");
		// ruleIds.add("1-1-1-6");
		// ruleIds.add("1-1-2-1");
		// ruleIds.add("1-2-3-1");
		// ruleIds.add("1-2-3-2");
		// ruleIds.add("1-2-3-3");
		ruleIds.add("1-1-2-1");
		DBObject doc1 = new BasicDBObject("$in", ruleIds);
		dbObject.put("rdList.ruleId", doc1);
		// dbObject.put("rdList.ruleId", "1-2-2-2");
		System.out.println("应用" + results.distinct("MD5_Apk", dbObject).size());
		DBCursor dbCursor = results.find(dbObject);
		System.out.println("总共" + dbCursor.count());
		// dbCursor.skip(227050);
		// System.out.println(dbCursor.count());
		Set<String> md5Set = new HashSet<String>();
		while (dbCursor.hasNext()) {
			DBObject resultDbObject = dbCursor.next();
			String md5 = (String) resultDbObject.get("MD5_Apk");
			if (md5Set.contains(md5)) {
				continue;
			}
			md5Set.add(md5);
		}
		System.out.println(md5Set.size());
		writeFile(md5Set, "E:\\实验室\\项目\\数据库\\6月17日和游戏统计\\68md5.txt");
	}

	static void getVirusApk() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("app.antivirus.result");
		BasicDBList values = new BasicDBList();
		values.add(new BasicDBObject("positives", new BasicDBObject("$gt", 1)));
		// DBCursor dbCursor = results.find(values).limit(50);
		DBCursor dbCursor = results.find().limit(10000);
		while (dbCursor.hasNext()) {
			DBObject resultDbObject = dbCursor.next();
			int positives = Integer.valueOf((String) resultDbObject.get("positives"));
			if (positives >= 30) {
				String md5 = (String) resultDbObject.get("md5");
				System.out.println(md5);
			}
		}
	}

	static void virusStatistic() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("app.antivirus.result");
		BasicDBList checksum = new BasicDBList();
		List<String> fileChecksums = getFileChecksumList("E:\\program\\报告服务\\ios\\virusmd5.txt");
		System.out.println("md5 共有" + fileChecksums.size());
		for (String fileChecksum : fileChecksums) {
			checksum.add(fileChecksum);
		}
		DBObject doc = new BasicDBObject("$in", checksum);
		BasicDBObject dbObject = new BasicDBObject("md5", doc);
		DBCursor dbCursor = results.find(dbObject);
		String resultString = "";
		System.out.println("总共" + dbCursor.count());
		// AppStorageService appStorageService = new AppStorageService();
		Set<String> md5Set = new HashSet<String>();
		while (dbCursor.hasNext()) {
			DBObject resultDbObject = dbCursor.next();
			String md5 = (String) resultDbObject.get("md5");
			if (md5Set.contains(md5)) {
				continue;
			}
			md5Set.add(md5);
			// resultString += "文件名：" +
			// appStorageService.getFileNameByFileChecksum(md5) + "\n";
			resultString += "md5:" + md5 + "\n";
			BasicDBList scansBasicDBList = (BasicDBList) resultDbObject.get("scans");
			int j = 0;
			for (int i = 0; i < scansBasicDBList.size(); i++) {
				BasicDBObject basicDBObject = (BasicDBObject) scansBasicDBList.get(i);
				BasicDBObject scanResultObject = (BasicDBObject) basicDBObject.get("scanResult");
				if (!scanResultObject.get("result").equals("None")) {
					resultString += basicDBObject.get("antivirusVendor") + "\t" + scanResultObject.get("result") + "\n";
					j++;
				}
			}
			resultString += "引擎数：" + j + "\n";
			// resultlist.add(appAntivirusResult);
		}
		File file = new File("E:\\result.txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
				// 写入内容
				BufferedWriter output = new BufferedWriter(new FileWriter(file));
				output.write(resultString);
				output.newLine();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	// 查看多少款应用触发了Android.Riskware.SMSReg
	static void virusStatistic1() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("app.antivirus.result");
		BasicDBList checksum = new BasicDBList();
		List<String> fileChecksums = getFileChecksumList("E:\\实验室\\项目\\数据库\\6月17日和游戏统计\\病毒检测有毒md5.txt");
		System.out.println("md5 共有" + fileChecksums.size());
		for (String fileChecksum : fileChecksums) {
			checksum.add(fileChecksum);
		}
		DBObject doc = new BasicDBObject("$in", checksum);
		BasicDBObject dbObject = new BasicDBObject("md5", doc);
		DBCursor dbCursor = results.find(dbObject);
		System.out.println("总共" + dbCursor.count());
		// AppStorageService appStorageService = new AppStorageService();
		Set<String> md5Set = new HashSet<String>();
		Set<String> md5Set1 = new HashSet<String>();
		int count = 0;
		while (dbCursor.hasNext()) {
			boolean isSms = false;
			DBObject resultDbObject = dbCursor.next();
			// resultString += "文件名：" +
			// appStorageService.getFileNameByFileChecksum(md5) + "\n";
			// resultString += "md5:" + md5 + "\n";
			String md5 = (String) resultDbObject.get("md5");
			if (md5Set.contains(md5)) {
				continue;
			}
			md5Set.add(md5);
			BasicDBList scansBasicDBList = (BasicDBList) resultDbObject.get("scans");
			for (int i = 0; i < scansBasicDBList.size(); i++) {
				BasicDBObject basicDBObject = (BasicDBObject) scansBasicDBList.get(i);
				BasicDBObject scanResultObject = (BasicDBObject) basicDBObject.get("scanResult");
				if (!scanResultObject.get("result").equals("None")) {
					if (scanResultObject.get("result").toString().contains("SMSreg")) {
						isSms = true;
						md5Set1.add(md5);
					}
				}
			}
			if (isSms) {
				count++;
			} else {
				// md5Set1.add(md5);
			}
			// resultlist.add(appAntivirusResult);
		}
		System.out.println("去重后:" + md5Set.size());
		System.out.println(count);
		System.out.println(md5Set1.size());
		writeFile(md5Set1, "E:\\实验室\\项目\\数据库\\6月17日和游戏统计\\含SMSReg病毒md5.txt");

	}

	// 查看病毒检测报错引擎数大于10，15，20的md5
	static void virusStatistic2() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("app.antivirus.result");
		BasicDBList checksum = new BasicDBList();
		List<String> fileChecksums = getFileChecksumList("E:\\实验室\\项目\\深圳\\11月\\md5\\结果\\yyk.txt");
		System.out.println("md5 共有" + fileChecksums.size());
		for (String fileChecksum : fileChecksums) {
			checksum.add(fileChecksum);
		}
		DBObject doc = new BasicDBObject("$in", checksum);
		BasicDBObject dbObject = new BasicDBObject("md5", doc);
		DBCursor dbCursor = results.find(dbObject);
		String resultString = "";
		System.out.println("总共" + dbCursor.count());
		// AppStorageService appStorageService = new AppStorageService();
		Set<String> md5Set = new HashSet<String>();
		int virusCount = 0;
		while (dbCursor.hasNext()) {
			DBObject resultDbObject = dbCursor.next();
			String md5 = (String) resultDbObject.get("md5");
			if (md5Set.contains(md5)) {
				continue;
			}
			md5Set.add(md5);
			BasicDBList scansBasicDBList = (BasicDBList) resultDbObject.get("scans");
			int j = 0;
			for (int i = 0; i < scansBasicDBList.size(); i++) {
				BasicDBObject basicDBObject = (BasicDBObject) scansBasicDBList.get(i);
				BasicDBObject scanResultObject = (BasicDBObject) basicDBObject.get("scanResult");
				if (!scanResultObject.get("result").equals("None")) {
					// resultString += basicDBObject.get("antivirusVendor") +
					// "\t" + scanResultObject.get("result") + "\n";
					j++;
				}
			}
			if (j >= 18) {
				virusCount++;
				resultString += md5 + "\n";
				// resultString+="引擎数："+j+"\n";
			}
			// resultlist.add(appAntivirusResult);
		}
		File file = new File("E:\\实验室\\项目\\深圳\\11月\\md5\\结果\\yyk1.txt");
		if (file.exists()) {
			file.delete();
			try {
				file.createNewFile();
				// 写入内容
				BufferedWriter output = new BufferedWriter(new FileWriter(file));
				output.write(resultString);
				output.newLine();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				file.createNewFile();
				// 写入内容
				BufferedWriter output = new BufferedWriter(new FileWriter(file));
				output.write(resultString);
				output.newLine();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(virusCount);
	}
	// static void virusTypeStatistic(String appStoreName, List<String>
	// fileChecksumList) {
	// System.out.println("appStoreName : " + appStoreName);
	// WritableWorkbook workBook =
	// ExportExcel.createWorkBook("E:\\实验室\\项目\\深圳\\11月\\应用酷\\virus" +
	// appStoreName);
	// WritableSheet sheet = ExportExcel.createSheet(workBook);
	// BasicDBList checksum = new BasicDBList();
	// for(String fileChecksum : fileChecksumList) {
	// checksum.add(fileChecksum);
	// }
	// DBObject doc = new BasicDBObject("$in", checksum);
	// BasicDBObject dbObject = new BasicDBObject("md5", doc);
	// DBCollection results =
	// MongodbConnFactory.getCollection("app.antivirus.result");
	// DBCursor dbCursor = results.find(dbObject);
	//
	// Set<String> checkedMd5s = new HashSet<String>();
	//
	// int sum = 0;
	// int appSum = 0;
	// while (dbCursor.hasNext()) {
	//
	// Set<String> virusTypeSet = new HashSet<String>();
	// DBObject resultDbObject = dbCursor.next();
	// String md5 = (String) resultDbObject.get("md5");
	// System.out.println("md5 : " + md5);
	// if (checkedMd5s.contains(md5)) {
	// continue;
	// } else {
	// appSum++;
	// }
	// checkedMd5s.add(md5);
	//
	// BasicDBList scansBasicDBList = (BasicDBList) resultDbObject.get("scans");
	// Map<String, String> virusTypeMap = new HashMap<String, String>();
	// for (int i = 0; i < scansBasicDBList.size(); i++) {
	// BasicDBObject basicDBObject = (BasicDBObject) scansBasicDBList.get(i);
	// String vendor = (String) basicDBObject.get("antivirusVendor");
	//// if(!(vendor.contains("Kingsoft") || vendor.contains("Qihoo-360") ||
	// vendor.contains("Tencent") || vendor.contains("Antiy-AVL") ||
	// vendor.contains("Baidu-International"))) {
	//// continue;
	//// }
	// BasicDBObject scanResultObject = (BasicDBObject)
	// basicDBObject.get("scanResult");
	//// if (!scanResultObject.get("result").equals("None") &&
	// !scanResultObject.get("result").toString().contains("SMSreg")) {
	// if (!scanResultObject.get("result").equals("None")) {
	// //virusTypeSet.add(scanResultObject.get("result").toString());
	// String virustType = scanResultObject.get("result").toString();
	// virusTypeMap.put(vendor, virustType);
	// }
	// }
	//// for (String virusType : virusTypeSet) {
	//// if (!virusTypeCountMap.containsKey(virusType)) {
	//// virusTypeCountMap.put(virusType, 0);
	//// }
	//// int virusTypeCount = virusTypeCountMap.get(virusType) + 1;
	//// virusTypeCountMap.remove(virusType);
	//// virusTypeCountMap.put(virusType, virusTypeCount);
	//// }
	//// if (virusTypeSet.size() > 0) {
	//// sum++;
	//// }
	// ExportExcel.exportExcel(workBook, sheet, md5, virusTypeMap);
	// virusTypeMap.clear();
	// }
	// try {
	// workBook.write();
	// workBook.close();
	// } catch (WriteException | IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//// System.out.println(appSum + ":" + sum);
	//
	//
	// }

	// 取这些md5对应的应用触发的规则数
	@SuppressWarnings("unused")
	static void ruleNumberStatistic() throws IOException {
		DBCollection results = MongodbConnFactory.getCollection("android.static.result");
		// BasicDBObject doc=new BasicDBObject();
		BasicDBList checksum = new BasicDBList();
		List<String> fileChecksums = getFileChecksumList("E:\\实验室\\项目\\数据库\\6月17日和游戏统计\\md5.txt");
		System.out.println("md5 共有" + fileChecksums.size());
		for (String fileChecksum : fileChecksums) {
			checksum.add(fileChecksum);
		}
		String resultString = "";
		// AppStorageService appStorageService = new AppStorageService();
		DBObject doc = new BasicDBObject("$in", checksum);
		// 新一点的应用是MD5_Apk
		BasicDBObject dbObject = new BasicDBObject("MD5_Apk", doc);
		DBCursor dbCursor = results.find(dbObject);
		// BasicDBList basicDBList=(BasicDBList)
		// results.distinct("MD5_Apk",dbObject);
		System.out.println("总共:" + dbCursor.count());
		// System.out.println(basicDBList.get(0));
		// for(int count=0;count < basicDBList.size();count++){
		Set<String> md5Set = new HashSet<String>();
		while (dbCursor.hasNext()) {
			DBObject resultObject = dbCursor.next();
			// DBObject resultObject=(DBObject) basicDBList.get(count);
			String md5 = (String) resultObject.get("MD5_Apk");
			if (md5Set.contains(md5)) {
				continue;
			}
			md5Set.add(md5);
			// resultString += "文件名：" +
			// appStorageService.getFileNameByFileChecksum(md5) + "\n";
			resultString += "md5:" + md5 + "\n";
			BasicDBList rdList = (BasicDBList) resultObject.get("rdList");
			int j = 0;
			Set<String> ruleIdSet = new HashSet<String>();
			for (int i = 0; i < rdList.size(); i++) {
				BasicDBObject basicDBObject = (BasicDBObject) rdList.get(i);
				String ruleId = (String) basicDBObject.get("ruleId");
				if (ruleId.startsWith("3-") || ruleId.indexOf("/3-") != -1) {
				} else {
					j++;
					resultString += ruleId + "\n";
					ruleIdSet.add(ruleId);
				}

			}
			// System.out.println(ruleIdSet.size());
			/*
			 * Iterator<String> iterator = ruleIdSet.iterator(); while
			 * (iterator.hasNext()) { resultString += iterator.next() + "\n"; }
			 */
			resultString += "规则数：" + ruleIdSet.size() + "\n";
			// resultString += "规则数："+j + "\n";
		}
		File file = new File("E:\\novirusresult.txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
				// 写入内容
				BufferedWriter output = new BufferedWriter(new FileWriter(file));
				output.write(resultString);
				output.newLine();
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	@SuppressWarnings("resource")
	static void ruleFileStatistic(String fileName) {
		File file = new File(fileName);
		String resultString = "";
		try {
			FileReader f_reader = new FileReader(file);
			BufferedReader reader = new BufferedReader(f_reader);
			String str = reader.readLine();
			while (str != null) {
				resultString += str + "\n";
				str = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int count = 0;
		int count1 = 0;
		String[] strings = resultString.split("文件名：");
		for (int i = 1; i < strings.length; i++) {
			List<String> ruleIdList = new ArrayList<String>();
			String[] ruleIds = strings[i].split("md5:")[1].split("\n");
			for (int j = 1; j < ruleIds.length; j++) {
				if (!ruleIds[j].startsWith("触发规则")) {
					ruleIdList.add(ruleIds[j]);
				}

			}
			if (ruleIdList.contains("1-1-2-1") && ruleIdList.contains("1-1-2-2") && ruleIdList.contains("1-1-2-3")) {
				count++;
			}
			if (ruleIdList.contains("1-1-1-2") && ruleIdList.contains("1-2-1-2") && ruleIdList.contains("1-2-1-3")) {
				count1++;
			}
		}
		System.out.println("第一个：" + count + " 第二个：" + count1);

	}

	static List<String> getFileChecksumList(String filename) throws IOException {
		List<String> virusFileNameList = new ArrayList<String>();
		File nameFile = new File(filename);
		FileInputStream namefileInputStream = new FileInputStream(nameFile);
		BufferedReader dr = new BufferedReader(new InputStreamReader(namefileInputStream, "UTF-8"));
		String line = dr.readLine();
		while (line != null) {
			virusFileNameList.add(line);
			line = dr.readLine();
		}
		namefileInputStream.close();
		dr.close();
		return virusFileNameList;

	}

	static Set<String> getFileChecksumList1(String filename) throws IOException {
		Set<String> fileNameSet = new HashSet<String>();
		File nameFile = new File(filename);
		FileInputStream namefileInputStream = new FileInputStream(nameFile);
		BufferedReader dr = new BufferedReader(new InputStreamReader(namefileInputStream, "UTF-8"));
		String line = dr.readLine();
		while (line != null) {
			fileNameSet.add(line);
			line = dr.readLine();
		}
		namefileInputStream.close();
		dr.close();
		return fileNameSet;

	}

	// public static Set<ApkUrlStrings> getApkUrl(Set<String> fileChecksumList)
	// {
	// Set<ApkUrlStrings> apkUrlStringsSet = new HashSet<ApkUrlStrings>();
	// DBCollection urlCollection =
	// MongodbConnFactory.getCollection("android.url.result");
	// BasicDBList checksumList = new BasicDBList();
	// for (String fileChecksum : fileChecksumList) {
	// checksumList.add(fileChecksum);
	// }
	// DBObject basicQuery = new BasicDBObject("$in", checksumList);
	// DBObject query = new BasicDBObject();
	// query.put("apk_md5", basicQuery);
	// DBCursor dbCursor = urlCollection.find(query).sort(new
	// BasicDBObject("timestamp", -1));
	// while (dbCursor.hasNext()) {
	// DBObject resultDbObject = dbCursor.next();
	// List<String> urlList = (List<String>) resultDbObject.get("apkUrlList");
	// if (urlList != null && urlList.size() != 0) {
	// String md5 = (String) resultDbObject.get("apk_md5");
	// long timestamp = (long) resultDbObject.get("timestamp");
	// ApkUrlStrings apkUrlStrings = new ApkUrlStrings();
	// apkUrlStrings.setApk_md5(md5);
	// apkUrlStrings.setApkUrlList(urlList);
	// apkUrlStrings.setTimestamp(timestamp);
	// apkUrlStringsSet.add(apkUrlStrings);
	// }
	// }
	// return apkUrlStringsSet;
	// }

	private static int writeFile(Set<String> fileChecksumList, String targetFilePath) {
		System.out.println(fileChecksumList.size());
		File targetFile = new File(targetFilePath);
		if (!targetFile.exists()) {
			try {
				targetFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		BufferedWriter output = null;
		try {
			output = new BufferedWriter(new FileWriter(targetFile));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Iterator<String> fileChecksums = fileChecksumList.iterator();
		while (fileChecksums.hasNext()) {
			String fileChecksum = fileChecksums.next();
			String firstDir = fileChecksum.substring(0, 2);
			String secondDir = fileChecksum.substring(2, 4);
			String filePath = "analysis/apk/" + firstDir + "/" + secondDir + "/" + fileChecksum;
			try {
				output.write(filePath + ".apk" + "\n");
				// output.write(fileChecksum + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
